package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.vo.FriendsReqVO;
	import com.rogo.rogobox.views.components.events.PageEvent;
	import com.rogo.rogobox.views.components.pages.PageFriend;
	import com.sina.microblog.data.MicroBlogUser;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	public class PageFriendMediator extends Mediator
	{
		public function PageFriendMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'pageFriendMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var currentUser:MicroBlogUser;
		private var vo:FriendsReqVO;
		private var currentFlag:Boolean = false;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			vo = new FriendsReqVO();
			view.pageBar.addEventListener(PageEvent.CHANGE, pageBar_changeHandler);
			view.previous.addEventListener(MouseEvent.CLICK, previous_clickHandler);
			
			updateDisplay();
		}
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.DISPLAY_USER_FRIENDS,
				AppFacade.DISPLAY_USER_FOLLOWER,
				AppFacade.LOAD_FOLLOWER_RESULT,
				AppFacade.LOAD_FRIENDS_RESULT];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch(notification.name)
			{
				case AppFacade.DISPLAY_USER_FOLLOWER:
				case AppFacade.DISPLAY_USER_FRIENDS:
					updateDisplay();
					break;
				default :
					view.dataGroup.dataProvider = new ArrayCollection(notification.param as Array);
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function get view():PageFriend
		{
			return viewComponent as PageFriend;
		}
		
		private function updateDisplay():void
		{
			if (currentUser && 
				currentUser.id == App.getInstance().friendPageUser.id &&
				currentFlag == App.getInstance().friendPageIsFollower)
			{
				return;
			}
			
			currentUser = App.getInstance().friendPageUser;
			currentFlag = App.getInstance().friendPageIsFollower;
			var user:MicroBlogUser = App.getInstance().verifyUser;
			
			if (currentUser.id == user.id)
			{
				view.title.text = currentFlag ? '我的粉丝' : '我关注的人';
			}
			else
			{
				view.title.text = currentFlag ? currentUser.screenName + '的粉丝' : currentUser.screenName + '关注的人';
			}
			
			vo.user = currentUser.id;
			view.pageBar.page = 1;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function pageBar_changeHandler(e:PageEvent):void
		{
			vo.cursor = --e.page * vo.count;
			
			if (currentFlag)
				sendNotification(AppFacade.LOAD_FOLLOWER, vo);
			else
				sendNotification(AppFacade.LOAD_FRIENDS, vo);
		}
		
		private function previous_clickHandler(e:MouseEvent):void
		{
			sendNotification(AppFacade.BACK_CONTENT_VIEW);
		}
		
	}
}